﻿// <copyright file="MySpaceVideosRequest.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-10-07</date>
// <summary>CLR Object for MySpace Videos Silverlight data binding</summary>

namespace MyOpenSpace
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Windows.Browser;
    using System.Windows.Media.Imaging;
    using Synergist;

    /// <summary>
    /// The IDKey for the scope of the query
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ID", Justification = "Mirroring opensocial API.")]
    public enum IDKey
    {
        /// <summary>
        /// Query the Viewer's videos
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", 
            MessageId = "VIEWER", Justification = "Mirroring opensocial API.")]
        VIEWER,

        /// <summary>
        /// Query the Owner's videos
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", 
            MessageId = "OWNER", Justification = "Mirroring opensocial API.")]
        OWNER
    }

    /// <summary>
    /// MySpace Videos CLR object to be used in Silverlight Data Binding
    /// </summary>
    public class MySpaceVideosRequest : INotifyPropertyChanged
    {
        /// <summary>
        /// the ID of the user to query for videos
        /// </summary>
        private string idSpec = "opensocial.IdSpec.PersonId.OWNER";

        /// <summary>
        /// Initializes a new instance of the MySpaceVideosRequest class.
        /// </summary>
        public MySpaceVideosRequest()
        {
            this.Videos = new ObservableCollection<Video>();

            if (!HtmlPage.IsEnabled)
            {
                this.AddSampleData();
            }
        }

        /// <summary>
        /// property changed event
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the entity to query (Owner or Viewer)
        /// </summary>
        [Description("opensocial.IdSpec.PersonId.OWNER for the owner's friends\nopensocial.IdSpec.PersonId.VIEWER for viewer's friends")]
        public string IdSpec
        { 
            get
            {
                return this.idSpec;
            }

            set
            {
                this.idSpec = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("IdSpec"));
                }

                this.FetchVideos();
            }
        }

        /// <summary>
        /// Gets the video collection
        /// </summary>
        [Description("the video collection")]
        public ObservableCollection<Video> Videos { get; private set; }

        /// <summary>
        /// Fetch the videos
        /// </summary>
        private void FetchVideos()
        {
            var container = opensocial.Container.get();

            if (container == null)
            {
                this.AddSampleData();

                return;
            }

            var request = container.newDataRequest();

            var personRequest = container.newFetchVideosRequest(this.IdSpec.Eval(), null);

            request.add(personRequest);

            request.send(new opensocial.DataReceived(this.OnDataReceived));
        }

        /// <summary>
        /// Add sample data
        /// </summary>
        private void AddSampleData()
        {
            if (this.Videos.Count > 0)
            {
                return;
            }

            this.Videos.Add(new Video() { Title  = Properties.Resources.VideoTitle, Description = Properties.Resources.VideoDescription, ThumbImage = new BitmapImage(new Uri(Properties.Resources.VideoThumbnailUri, UriKind.Absolute)) });
            this.Videos.Add(new Video() { Title = Properties.Resources.VideoTitle, Description = Properties.Resources.VideoDescription, ThumbImage = new BitmapImage(new Uri(Properties.Resources.VideoThumbnailUri, UriKind.Absolute)) });
            this.Videos.Add(new Video() { Title = Properties.Resources.VideoTitle, Description = Properties.Resources.VideoDescription, ThumbImage = new BitmapImage(new Uri(Properties.Resources.VideoThumbnailUri, UriKind.Absolute)) });
        }

        /// <summary>
        /// Videos are returned from MySpace
        /// </summary>
        /// <param name="response">the response from the web service call</param>
        private void OnDataReceived(opensocial.DataResponse response)
        {
            if (response.HadError)
            {
                System.Diagnostics.Debug.WriteLine("Error requesting videos: {0}", response.ErrorMessage);

                return;
            }

            var data = response.get("MyOpenSpace.RequestType.FETCH_VIDEOS".Eval()).getData();

            var collection = new opensocial.Collection(data);

            var viewerVideos = collection.asArray();

            var videos = from scriptObject in viewerVideos
                          select new Video(scriptObject);

            this.Videos.Clear();

            foreach (var video in videos)
            {
                this.Videos.Add(video);
            }
        }
    }
}
